﻿Imports System.Data.OleDb
Public Class frmThemLoaiSanPham
#Region "Form Event"
    Public IDLoai As Integer = -1
    Private Sub frmThemLoaiSanPham_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            txtMaLoai.Text = ""
            txtLoaiSanPham.Text = ""
            txtDonViTinh.Text = ""
            txtGhiChu.Text = ""
            If IDLoai <> -1 Then
                Dim dbConn As New OleDb.OleDbConnection
                Dim dataAccess As New DataAccess
                Dim dsNhanVienTiepThi As New DataSet
                Dim dtNhanVienTiepThi As New DataTable
                Dim query As String = "Select MaLoai,TenLoai,GhiChu,DonViTinh From LoaiSanPham Where IDLoai=" & IDLoai
                Dim adapter As OleDb.OleDbDataAdapter
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                adapter = New OleDb.OleDbDataAdapter(query, dbConn)
                adapter.Fill(dsNhanVienTiepThi)
                dbConn.Close()
                dtNhanVienTiepThi = dsNhanVienTiepThi.Tables(0)
                txtMaLoai.Text = dtNhanVienTiepThi.Rows(0)("MaLoai")
                txtLoaiSanPham.Text = dtNhanVienTiepThi.Rows(0)("TenLoai")
                txtDonViTinh.Text = IIf(IsDBNull(dtNhanVienTiepThi.Rows(0)("DonViTinh")), "", dtNhanVienTiepThi.Rows(0)("DonViTinh"))
                txtGhiChu.Text = dtNhanVienTiepThi.Rows(0)("GhiChu")
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Button Event"
    Private Sub txtMaLoai_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMaLoai.KeyUp
        Try
            txtMaLoai.BackColor = Color.White
        Catch ex As Exception

        End Try
    End Sub

    Private Sub txtLoaiSanPham_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtLoaiSanPham.KeyUp
        Try
            txtLoaiSanPham.BackColor = Color.White
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            If txtMaLoai.Text = "" Then
                EmptyData(txtMaLoai)
                Exit Sub
            End If
            If txtLoaiSanPham.Text = "" Then
                EmptyData(txtLoaiSanPham)
                Exit Sub
            End If
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim dbCommand As OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim query As String = ""
            If IDLoai = -1 Then
                query = String.Format("Insert Into LoaiSanPham(MaLoai,TenLoai,GhiChu,DonViTinh) Values(N'{0}',N'{1}',N'{2}',N'{3}')", txtMaLoai.Text, txtLoaiSanPham.Text, txtGhiChu.Text, txtDonViTinh.Text)
            Else
                query = String.Format("Update LoaiSanPham Set MaLoai=N'{0}',TenLoai=N'{1}',GhiChu=N'{2}',DonViTinh=N'{3}' Where IdLoai={4}", txtMaLoai.Text, txtLoaiSanPham.Text, txtGhiChu.Text, txtDonViTinh.Text, IDLoai)
            End If
            dbCommand = New OleDbCommand(query, dbConn)
            dbCommand.ExecuteNonQuery()
            dbConn.Close()
            txtMaLoai.Text = ""
            txtLoaiSanPham.Text = ""
            txtGhiChu.Text = ""
            txtDonViTinh.Text = ""
            frmLoaiSanPham.LoaiSanPham_Load()
            If IDLoai <> -1 Then
                Me.Close()
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Try
            txtMaLoai.Text = ""
            txtLoaiSanPham.Text = ""
            txtGhiChu.Text = ""
            frmLoaiSanPham.LoaiSanPham_Load()
            Me.Close()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Load Data"

#End Region

End Class